/*
 * Copyright (C) 2012 Jesse Webb (gweebz@gmail.com)
 *
 * This file is part of Gwahtzee.
 *
 * Gwahtzee is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gwahtzee is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gwahtzee.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.googlecode.gwahtzee.gui.mainmenu;

import org.newdawn.slick.Color;
import org.newdawn.slick.Image;

public abstract class Button {
    private static final float DEFAULT_IMAGE_SCALE = 1.0f;
    private static final float MAX_IMAGE_SCALE = 1.1f;

    private int x;
    private int y;
    private Image image;
    private boolean isMouseOver;
    private float imageScale;
    private float scaleStep;

    public Button(int x, int y, Image image) {
        if (image == null) {
            throw new IllegalArgumentException("Image can not be null");
        }
        this.x = x;
        this.y = y;
        this.image = image;
        this.isMouseOver = false;
        this.imageScale = DEFAULT_IMAGE_SCALE;
        this.scaleStep = 0.0002f;
    }

    public void update(int mouseX, int mouseY, int delta) {
        isMouseOver = (mouseX >= x && mouseX <= x + image.getWidth() &&
                mouseY >= y && mouseY <= y + image.getHeight());

        if (isMouseOver) {
            if (imageScale >= MAX_IMAGE_SCALE) {
                scaleStep = Math.abs(scaleStep) * -1;
            }
            if (imageScale <= DEFAULT_IMAGE_SCALE) {
                scaleStep = Math.abs(scaleStep);
            }
            imageScale += scaleStep * delta;
            if (imageScale > MAX_IMAGE_SCALE) imageScale = MAX_IMAGE_SCALE;
            else if (imageScale < DEFAULT_IMAGE_SCALE) imageScale = DEFAULT_IMAGE_SCALE;
        } else {
            if (imageScale > DEFAULT_IMAGE_SCALE) {
                scaleStep = Math.abs(scaleStep) * -1;
                imageScale += scaleStep * delta;
            } else {
                imageScale = DEFAULT_IMAGE_SCALE;
            }
        }
    }

    public void render() {
        Color color;
        if (isMouseOver) {
            color = Color.green;
        } else {
            color = Color.white;
        }
        image.draw(x, y, image.getWidth() * imageScale, image.getHeight() * imageScale, color);
    }

    public boolean isMouseOver() {
        return isMouseOver;
    }

    public abstract void click();
}
